﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Context;

namespace Revis.VClinicNet.Core.Domain.Persistence.Nh.Tests.Db
{
    public abstract class AbstractDatabase : ITestDatabase
    {
        protected Configuration _configuration = null;
        protected ISessionFactory _factory = null;

        public AbstractDatabase()
        {
            Configure();
        }

        public virtual void Configure()
        {
        }

        public virtual void ExportSchema()
        {
            new SchemaExport(_configuration).Execute(false, true, false);
        }

        public virtual ISessionFactory Factory
        {
            get { return _factory; }
        }

        public virtual void BindCurrentSessionContext()
        {
            CurrentSessionContext.Bind(Factory.OpenSession());
        }

        public virtual void UnbindCurrentSessionContext()
        {
            ISession session = CurrentSessionContext.Unbind(Factory);
            if (session == null) {
                session.Dispose();
            }
        }

        public virtual ISession Session
        {
            get { return Factory.GetCurrentSession(); }
        }
    }
}
